//
//  XQBaseCustomIdentityInfoController.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/26.
//

#import "XQBaseCustomIdentityInfoController.h"
#import "XQCustomOptionalPop.h"
#import "XQCityLocationPopView.h"

@interface XQBaseCustomIdentityInfoController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) XQButton *bottomButton;
@end

@implementation XQBaseCustomIdentityInfoController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self makeUI];
    [self makeConstraints];
    
    [self refreshData];
}

- (void)makeUI {
    self.view.backgroundColor = kCardColor;
    
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.bottomButton];
}

- (void)makeConstraints {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo([UIScreen mainScreen].topHeight + 20);
        make.left.equalTo(@12);
        make.right.equalTo(@-12);
        make.bottom.offset(-[UIScreen mainScreen].bottomHeight - 100);
    }];
    [self.bottomButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-[UIScreen mainScreen].bottomHeight - 20);
        make.left.right.equalTo(@0);
    }];
}

- (void)refreshData {}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataSource[indexPath.section];
    NSString *concept = dic[@"concept"];
    NSArray *array = dic[@"emissions"];
    
    XQCustomInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XQCustomInfoCell" forIndexPath:indexPath];
    [cell setPaper:dic[@"paper"]];
    [cell setPlaceholder:dic[@"document"]];
    if (array.count == 0) {
        [cell setContent:self.dataDic[concept]];
    }else {
        [cell setContent:[self getChance:concept dic:dic]];
    }
    [cell setIsNumberKeyboard:[dic[@"natural"] integerValue] == 1];
    @weakify(self);
    cell.buttonHandle = ^{
        @strongify(self);
        [self.view endEditing:YES];
        [self didSelectAtIndexPath:indexPath];
    };
    cell.textFielHandle = ^(NSString * _Nonnull text) {
        @strongify(self);
        [self textFiledWithTextAtIndexPath:indexPath content:text];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 54;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    NSDictionary *dic = self.dataSource[section];
    
    XQTableHeaderView *headerView = [[XQTableHeaderView alloc] init];
    [headerView setContent:dic[@"won"]];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 32;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    [self.view endEditing:YES];
}

#pragma mark - setter

- (void)setTopTitle:(NSString *)topTitle {
    [self.navigationBar setTitle:topTitle];
}

#pragma mark - private

- (void)didSelectAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = self.dataSource[indexPath.section];
    
    NSString *concept = dic[@"concept"];
    NSString *won = dic[@"won"];
    NSArray *array = dic[@"emissions"];
    
    NSMutableArray *totalArray = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        NSString *text = dic[@"chance"];
        if (text.length) {
            [totalArray addObject:text];
        }
    }
    
    NSString *value = self.dataDic[concept];
    
    // 如果是城市
    bool isCity = [concept isEqualToString:@"studies"] || [concept isEqualToString:@"company_city"];
    if (isCity) {
        totalArray = [[NSUserDefaults standardUserDefaults] objectForKey:@"KCitys"];
    }
    
    if (isCity) {
        [self showCityPop:totalArray value:value concept:concept section:indexPath.section];
        return;
    }
    
    XQCustomOptionalPop *popView = [[XQCustomOptionalPop alloc] initWithTitle:won datas:totalArray];
    if (value.length) {
        [popView setSelctData:[self getChance:concept dic:dic]];
    }else {
        [popView setSelctData:totalArray.firstObject];
    }
    
    XQPopupAnimView *anim = [[XQPopupAnimView alloc] initWithAnimationType:XQPopupAnimationCenter customView:popView];
    anim.needCover = YES;
    anim.makeCustomConstraints = ^(XQPopupAnimView *animView, UIView *customView) {
        [customView mas_remakeConstraints:^(MASConstraintMaker *make) {
            (void)make.center;
        }];
    };
    
    [anim showInView:self.view completion:^{}];
    
    @weakify(anim)
    popView.confimHandle = ^(NSString * _Nonnull data) {
        @strongify(anim);
        NSString *spaceMobile = [self getChooseSpaceMobile:data dic:dic];
        self.dataDic[concept] = spaceMobile;
        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
            [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
        [anim hideAndCompletion:^{}];
    };
}

- (void)showCityPop:(NSArray *)dataSource value:(NSString *)value concept:(NSString *)concept section:(NSInteger)section {
    XQCityLocationPopView *popView = [[XQCityLocationPopView alloc] init];
    if (value.length) {
        [popView setSelectData:value];
    }
    
    XQPopupAnimView *anim = [[XQPopupAnimView alloc] initWithAnimationType:XQPopupAnimationCenter customView:popView];
    anim.needCover = YES;
    anim.makeCustomConstraints = ^(XQPopupAnimView *animView, UIView *customView) {
        [customView mas_remakeConstraints:^(MASConstraintMaker *make) {
            (void)make.center;
        }];
    };
    
    [anim showInView:self.view completion:^{}];
    
    @weakify(anim)
    popView.confimHandle = ^(NSString * _Nonnull data) {
        @strongify(anim);
        self.dataDic[concept] = data;
        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:section];
            [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
        [anim hideAndCompletion:^{}];
    };
    popView.closeHandle = ^{
        @strongify(anim);
        [anim hideAndCompletion:^{}];
    };
}

- (void)textFiledWithTextAtIndexPath:(NSIndexPath *)indexPath content:(NSString *)content{
    NSDictionary *dic = self.dataSource[indexPath.section];
    
    NSString *concept = dic[@"concept"];
    
    self.dataDic[concept] = content;
}

- (NSString *)getChooseSpaceMobile:(NSString *)data dic:(NSDictionary *)dic {
    NSString *spacemobile = @"";
    NSArray *list = dic[@"emissions"];
    if (list.count) {
        for (NSDictionary *subDic in list) {
            if ([subDic[@"chance"] isEqualToString:data]) {
                spacemobile = [subDic[@"spacemobile"] description];
                break;
            }
        }
        return spacemobile;
    }else {
        return data;
    }
}

- (NSString *)getChance:(NSString *)concept dic:(NSDictionary *)dic {
    NSString *chance = @"";
    NSString *spacemobile = self.dataDic[concept];
    
    NSArray *list = dic[@"emissions"];
    for (NSDictionary *subDic in list) {
        if ([[subDic[@"spacemobile"] description] isEqualToString:spacemobile]) {
            chance = [subDic[@"chance"] description];
            break;
        }
    }
    return chance;
}

- (void)clickNextButton {}

#pragma mark - lazy

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.layer.cornerRadius = 15;
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView setContentInset:UIEdgeInsetsMake(20, 0, 0, 0)];
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [_tableView registerClass:[XQCustomInfoCell class] forCellReuseIdentifier:@"XQCustomInfoCell"];
    }
    return _tableView;
}

- (XQButton *)bottomButton {
    if (!_bottomButton) {
        _bottomButton = [[XQButton alloc] initWithTitle:@"Next"];
        @weakify(self);
        _bottomButton.onTapHandle = ^{
            @strongify(self);
            [self clickNextButton];
        };
    }
    return _bottomButton;
}

- (NSArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSArray array];
    }
    return _dataSource;
}

- (NSMutableDictionary *)dataDic {
    if (!_dataDic) {
        _dataDic = [NSMutableDictionary dictionary];
    }
    return _dataDic;
}

@end
